package com.avic.rong.activity.mine;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.avic.rong.BaseActivity;
import com.avic.rong.R;
import com.avic.rong.api.mine.UpNameApi;
import com.avic.rong.utils.SharedPreferencesUtil;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.ToastTools;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;
import com.luck.picture.lib.dialog.CustomDialog;

import butterknife.BindView;
import butterknife.OnClick;

//账号设置-个人信息-修改昵称
public class UpNameActivity extends BaseActivity {

    @BindView(R.id.edittext_nickName)
    EditText edittext_nickName;
    @BindView(R.id.iv_clean)
    ImageView iv_clean;
    private String name;
    private Intent intent;
    private String oldnickName;


    @Override
    protected int getContentViewId() {
        return R.layout.activity_up_name;
    }

    @Override
    protected void initView() {
        tvTitle.setText("修改昵称");
        tvRight.setText("确定");
        tvRight.setTextColor(getResources().getColor(R.color.white));

        intent = getIntent();
        oldnickName = intent.getStringExtra("nickName");
        edittext_nickName.setText(oldnickName);
    }

    @Override
    protected void addListener() {
        tvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String trim = edittext_nickName.getText().toString().trim();
                if (trim.equals(oldnickName)){
                    finish();
                }else {
                    showDialog();
                }
            }
        });

        edittext_nickName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                tvRight.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (s.length()!=0){
                            upName();
                        }else {
                            finish();
                        }
                    }
                });
            }
        });

    }

    public static Intent queryIntent(Context context) {
        Intent intent = new Intent(context, UpNameActivity.class);
        return intent;
    }

    @OnClick({ R.id.edittext_nickName, R.id.iv_clean})
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.iv_clean:
                edittext_nickName.setText("");
                break;
        }
    }

    private void upName() {
        String login_token = SharedPreferencesUtil.getLoginToken();
        name = edittext_nickName.getText().toString().trim();
        UpNameApi upNameApi = new UpNameApi(this,upNameListener );
        upNameApi.setArgument(login_token, name);
        HttpManager.getInstance().doHttpDeal(upNameApi);

    }

    HttpOnNextListener<BaseResultEntity> upNameListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity baseResultEntity) {
            if (baseResultEntity.getCode().equals(ConstantsUtils.STATUS_SUCCESS_0000)){
                ToastTools.showPublicToast("修改成功");
                SharedPreferencesUtil.getInstance().setUserNickName(name);
                intent.putExtra("nickName_new",name);
                setResult(100,intent);
                finish();
            }else {
                Toast.makeText(UpNameActivity.this, baseResultEntity.getMsg(), Toast.LENGTH_SHORT).show();
            }
        }
    };

    private void showDialog() {
        CustomDialog dialog = new CustomDialog(this, R.layout.dialog_upname);
        dialog.show();
        dialog.setCanceledOnTouchOutside(false);
        dialog.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.cancel();
            }
        });



        dialog.findViewById(R.id.tv_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }
}
